unit BrCstOrd;

interface

uses
  SysUtils, Windows, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, DBTables, DB, DBCtrls, StdCtrls, ExtCtrls, Grids, DBGrids,
  Buttons, patterns;

type
  TBrCustOrdForm = class(TForm)
    CtrlsPanel: TPanel;
    Navigator: TDBNavigator;
    BtnPanel: TPanel;
    EditBtn: TButton;
    CloseBtn: TButton;
    ActivateBtn: TSpeedButton;
    DefineBtn: TSpeedButton;
    CustPanel: TPanel;
    CustGrid: TDBGrid;
    OrdersPanel: TPanel;
    OrdersGrid: TDBGrid;
    Bevel1: TBevel;
    procedure CustGridEnter(Sender: TObject);
    procedure CloseBtnClick(Sender: TObject);
    procedure OrdersGridEnter(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    FActiveSource: TDataSource;
    function GetCustNo: Double;
    function GetOrderNo: Double;
    procedure SetActiveSource(DataSource: TDataSource);
    procedure SetCustNo(NewCustNo: Double);
    procedure SetOrderNo(NewOrderNo: Double);
    property ActiveSource: TDataSource read FActiveSource write SetActiveSource;
  public
    property CustNo: Double read GetCustNo write SetCustNo;
    property OrderNo: Double read GetOrderNo write SetOrderNo;
    procedure setEventListner(AEventHandler: TNotifyEvent);
  end;

  TControllerCustOrd = class(TController)
  private
    procedure ActivateQuery(Sender: TObject);
  protected
    procedure DoCommand(Command: string; args: string); override;
  public
    constructor Create;
    procedure OnClick(Sender: TObject); //for ieventListener
  end;

var
  BrCustOrdForm: TBrCustOrdForm;

implementation

uses DataMod, commandlist, QryCust;

{$R *.DFM}

{ Retrieve from active customer table or query--whichever is active }

function TBrCustOrdForm.GetCustNo: Double;
begin
  Result := TMastData.getInstance.CustMasterSrc.Dataset.Fields[0].AsFloat;
end;

procedure TBrCustOrdForm.SetCustNo(NewCustNo: Double);
begin
  TMastData.getInstance.CustMasterSrc.Dataset := TMastData.getInstance.Cust;
  TMastData.getInstance.Cust.Locate('CustNo', NewCustNo, []);
end;

function TBrCustOrdForm.GetOrderNo: Double;
begin
  Result := TMastData.getInstance.OrdByCustOrderNo.Value;
end;

procedure TBrCustOrdForm.SetOrderNo(NewOrderNo: Double);
begin
  TMastData.getInstance.OrdByCust.Locate('OrderNo', NewOrderNo, []);
end;

procedure TBrCustOrdForm.SetActiveSource(DataSource: TDataSource);
begin
  FActiveSource := DataSource;
  Navigator.DataSource := FActiveSource;
end;

procedure TBrCustOrdForm.CustGridEnter(Sender: TObject);
begin
  ActiveSource := TMastData.getInstance.CustMasterSrc;
  CustGrid.Options := CustGrid.Options + [dgAlwaysShowSelection];
  OrdersGrid.Options := OrdersGrid.Options - [dgAlwaysShowSelection];
end;

procedure TBrCustOrdForm.OrdersGridEnter(Sender: TObject);
begin
  ActiveSource := TMastData.getInstance.OrdByCustSrc;
  OrdersGrid.Options := OrdersGrid.Options + [dgAlwaysShowSelection];
  CustGrid.Options := CustGrid.Options - [dgAlwaysShowSelection];
end;

procedure TBrCustOrdForm.CloseBtnClick(Sender: TObject);
begin
  Close;
end;

procedure TBrCustOrdForm.FormShow(Sender: TObject);
begin
  TMastData.getInstance.Cust.Open;
  TMastData.getInstance.Cust.First;
end;

procedure TBrCustOrdForm.setEventListner(AEventHandler: TNotifyEvent);
begin
  editBtn.onClick := AEventHandler;
  DefineBtn.onClick := AEventHandler;
  ActivateBtn.onClick := AEventHandler;
end;

//Controller

procedure TControllerCustOrd.DoCommand(Command: string; args: string);
begin
  if (Command = CMD_BTN_BROWSE) or (command = CMD_SHOW_ORDERS) then
    BrCustOrdForm.Show;
end;

procedure TControllerCustOrd.ActivateQuery(Sender: TObject);
begin
  with BrCustOrdForm do
  begin
    if not ActivateBtn.Down then
      TMastData.getInstance.CustMasterSrc.Dataset := TMastData.getInstance.Cust
    else
      with TMastData.getInstance.CustQuery do
      try
        Close;
        Params[0].AsDatetime := TQueryCustDlg.getInstance.FromDate;
        Params[1].AsDatetime := TQueryCustDlg.getInstance.ToDate;
        Open;
      { Any records in the result set? }
        if BOF and EOF then Abort;
        TMastData.getInstance.CustMasterSrc.Dataset := TMastData.getInstance.CustQuery;
      except
        TMastData.getInstance.CustMasterSrc.Dataset := TMastData.getInstance.Cust;
        ActivateBtn.Down := false;
        ShowMessage('No matching records in the specified date range.');
      end;
  end;
end;

constructor TControllerCustOrd.Create;
begin
  BrCustOrdForm := TBrCustOrdForm.Create(Application);
  BrCustOrdForm.setEventListner(self.OnClick);
end;

procedure TControllerCustOrd.OnClick(Sender: TObject); //for ieventListener
var
  F: TFloatField;
begin
  if (sender is TButton) then
  begin
    if (TButton(sender).Caption = '&Edit') then
    begin
      with BrCustOrdForm do
      begin
        F := ActiveSource.Dataset.Fields[0] as TFloatField;
        if ActiveSource = TMastData.getInstance.CustMasterSrc then
        begin
          self.SendCommand(CMD_EDIT_CUSTOMER, floatToStr(F.Value));
        end
        else
        begin
          self.SendCommand(CMD_EDIT_ORDER, floatToStr(F.Value));
        end;
      end;
    end
  end
  else if (Sender is TSpeedButton) then
  begin
    if (TButton(sender).Caption = '&Define Query') then
    begin
      if TQueryCustDlg.getInstance.ShowModal = mrOK then
        ActivateQuery(Self);
    end
    else if (TButton(sender).Caption = 'Activate Query') then
      ActivateQuery(sender);
  end;
end;

initialization
  ControlCenter.RegController(TControllerCustOrd.Create);

end.

